clear;

%%%%%%%%%%%%%%%%%%%%%%Data%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data = dataset('File','tableC5_data.csv','Delimiter',',');

iter = 1;
N = 615;
warning('off','all');
stations = [data.station_id1, data.station_id2, data.station_id3];

y1 = data.voteshare_vb_2014;
X10 = [ones(N,1)];
X11 = [ones(N,1) data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4];
X12 = [ones(N,1) data.voteshare_vb_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4];
X13 = [ones(N,1) data.voteshare_vb_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc];
X14 = [ones(N,1) data.voteshare_vb_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4  data.lit_pc data.rural_pc data.scst_pc data.state_election];
X15 = [ones(N,1) data.voteshare_vb_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election data.num_resp];

idx1 = find(~isnan(y1));

[beta,se,p,r2,F,control_mean,N,C,T] = fereg(y1(idx1),data.treat(idx1),X10(idx1,:),stations(idx1,:),2);
p_ri_fe = ri_fe(iter,beta,se,y1(idx1),X10(idx1,:),stations(idx1,:),2);
results1(:,1) = [beta(1);se(1);p(1);p_ri_fe(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = fereg(y1(idx1),data.treat(idx1),X11(idx1,:),stations(idx1,:),2);
p_ri_fe = ri_fe(iter,beta,se,y1(idx1),X11(idx1,:),stations(idx1,:),2);
results1(:,2) = [beta(1);se(1);p(1);p_ri_fe(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = fereg(y1(idx1),data.treat(idx1),X12(idx1,:),stations(idx1,:),2);
p_ri_fe = ri_fe(iter,beta,se,y1(idx1),X12(idx1,:),stations(idx1,:),2);
results1(:,3) = [beta(1);se(1);p(1);p_ri_fe(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = fereg(y1(idx1),data.treat(idx1),X13(idx1,:),stations(idx1,:),2);
p_ri_fe = ri_fe(iter,beta,se,y1(idx1),X13(idx1,:),stations(idx1,:),2);
results1(:,4) = [beta(1);se(1);p(1);p_ri_fe(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = fereg(y1(idx1),data.treat(idx1),X14(idx1,:),stations(idx1,:),2);
p_ri_fe = ri_fe(iter,beta,se,y1(idx1),X14(idx1,:),stations(idx1,:),2);
results1(:,5) = [beta(1);se(1);p(1);p_ri_fe(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = fereg(y1(idx1),data.treat(idx1),X15(idx1,:),stations(idx1,:),2);
p_ri_fe = ri_fe(iter,beta,se,y1(idx1),X15(idx1,:),stations(idx1,:),2);
results1(:,6) = [beta(1);se(1);p(1);p_ri_fe(1);r2;control_mean;N];

format bank;
disp(results1);
